package com.cis.education.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cis.education.domain.EduCourseTeacher;
import com.cis.education.domain.EduTeacher;
import com.cis.education.mapper.EduCourseTeacherMapper;
import com.cis.education.mapper.EduTeacherMapper;
import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cis.education.mapper.EduCourseMapper;
import com.cis.education.domain.EduCourse;
import com.cis.education.service.IEduCourseService;
import com.cis.common.core.text.Convert;

/**
 * 课程管理Service业务层处理
 * 
 * @author 管理員
 * @date 2022-01-11
 */
@Service
public class EduCourseServiceImpl implements IEduCourseService 
{
    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Autowired
    private EduCourseTeacherMapper eduCourseTeacherMapper;

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    /**
     * 查询课程管理
     * 
     * @param id 课程管理主键
     * @return 课程管理
     */
    @Override
    public EduCourse selectEduCourseById(Integer id)
    {
        return eduCourseMapper.selectEduCourseById(id);
    }

    /**
     * 查询任课教师关联
     * @param courseId
     * @return
     */
    @Override
    public EduCourseTeacher selectEduCourseTeacherByCourseId(Integer courseId) {
        return eduCourseTeacherMapper.selectEduCourseTeacherByCourseId(courseId);
    }

    /**
     * 查询课程管理列表
     * 
     * @param eduCourse 课程管理
     * @return 课程管理
     */
    @Override
    public List<EduCourse> selectEduCourseList(EduCourse eduCourse)
    {
        List<EduCourse> result = new ArrayList<>();
        List<EduCourse> eduCourses = eduCourseMapper.selectEduCourseList(eduCourse);
        for(EduCourse course : eduCourses){
            EduCourseTeacher eduCourseTeacher = eduCourseTeacherMapper.selectEduCourseTeacherByCourseId(course.getId());
            if(eduCourseTeacher != null){
                course.setTeacher(eduTeacherMapper.selectEduTeacherById(eduCourseTeacher.getTeacherId()));
            }
            result.add(course);
        }
        return result;
    }

    /**
     * 新增课程管理
     * 
     * @param eduCourse 课程管理
     * @return 结果
     */
    @Override
    public int insertEduCourse(EduCourse eduCourse)
    {
        int rows = eduCourseMapper.insertEduCourse(eduCourse);
        //新增任课教师
        insertEduTeacherCourse(eduCourse);
        return rows;
    }

    /**
     * 修改课程管理
     * 
     * @param eduCourse 课程管理
     * @return 结果
     */
    @Override
    public int updateEduCourse(EduCourse eduCourse)
    {
        Integer courseId = eduCourse.getId();
        eduCourseTeacherMapper.deleteEduCourseTeacherByCourseId(courseId);
        //新增任课教师
        insertEduTeacherCourse(eduCourse);
        return eduCourseMapper.updateEduCourse(eduCourse);
    }

    /**
     * 批量删除课程管理
     * 
     * @param ids 需要删除的课程管理主键
     * @return 结果
     */
    @Override
    public int deleteEduCourseByIds(String ids)
    {
        return eduCourseMapper.deleteEduCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程管理信息
     * 
     * @param courseId 课程管理主键
     * @return 结果
     */
    @Override
    public int deleteEduCourseById(Integer courseId)
    {
        //删除任课教师关联
        eduCourseTeacherMapper.deleteEduCourseTeacherByCourseId(courseId);
        return eduCourseMapper.deleteEduCourseById(courseId);
    }

    /**
     * 新增任课教师
     *
     * @param eduCourse 课程对象
     */
    private void insertEduTeacherCourse(EduCourse eduCourse) {
        EduTeacher teacher = eduCourse.getTeacher();
        EduCourseTeacher ct = new EduCourseTeacher();
        ct.setTeacherId(teacher.getId());
        ct.setCourseId(eduCourse.getId());
        eduCourseTeacherMapper.insertEduCourseTeacher(ct);
    }
}
